using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ProjectPheonix {
    public class Game1 : Microsoft.Xna.Framework.Game {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        EntityManager entityManager;
        public Random rnd { get; protected set; }
        public Player player { get; protected set; }
        Texture2D crossHWep1, crossHWep2, currentCross;
        float shotSpeed = 10;
        int shotDelay = 300;
        int shotCountdown = 0;

        public Game1() {
            rnd = new Random();
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

#if !DEBUG
            graphics.PreferredBackBufferWidth = 1600;
            graphics.PreferredBackBufferHeight = 900;
            graphics.IsFullScreen = false;
#endif

#if DEBUG
            graphics.PreferredBackBufferWidth = 1440;
            graphics.PreferredBackBufferHeight = 900;
            graphics.IsFullScreen = false;
#endif
        }

        protected override void Initialize() {
            player = new Player(this, new Vector3(0, 23, 50),
                Vector3.Zero, Vector3.Up);
            Components.Add(player);

            entityManager = new EntityManager(this);
            Components.Add(entityManager);

            base.Initialize();
        }

        protected override void LoadContent() {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            crossHWep1 = Content.Load<Texture2D>(@"textures\crosshair1");
            crossHWep2 = Content.Load<Texture2D>(@"textures\crosshair2");

            currentCross = crossHWep1;
        }

        protected override void UnloadContent() {

        }

        protected override void Update(GameTime gameTime) {

            if ((GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) ||
                (Keyboard.GetState().IsKeyDown(Keys.Escape)))
                this.Exit();

            FireShots(gameTime);

            base.Update(gameTime);
        }

        protected void FireShots(GameTime gameTime) {
            //If enough time (delay) has passed
            if (shotCountdown <= 0) {
                // Did player press space bar or left mouse button?
                if (Keyboard.GetState().IsKeyDown(Keys.Space) ||
                    Mouse.GetState().LeftButton == ButtonState.Pressed) {

                   
                        shotSpeed = .3f;
                        shotDelay = 300;
                        entityManager.AddShot(
                            player.playerPosition + new Vector3(0, -5, 0),
                            player.GetPlayerDirection * shotSpeed);


                    // Reset the shot countdown to the delay value
                    shotCountdown = shotDelay;
                }
            } else
                shotCountdown -= gameTime.ElapsedGameTime.Milliseconds;
        }

        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.Black);
            base.Draw(gameTime);
            spriteBatch.Begin();

            spriteBatch.Draw(currentCross, new Vector2((Window.ClientBounds.Width / 2) - (currentCross.Width / 2), (Window.ClientBounds.Height / 2) - (currentCross.Height / 2)), Color.White);

            spriteBatch.End();


            graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            graphics.GraphicsDevice.BlendState = BlendState.Opaque;

        }
    }
}
